import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/11.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
enum TrainUserCheckEnum {
  UserRecoder,
  trainInfo,
  trainPeriodInfo,
  trainCourseInfo,
  trainResultInfo,
  trainProblem,
  trainPlan,
  degreeOfMyRemark,
  degreeOfTrainRemark,
  teacherDegreeOfMyRemark,
}

///代码清单
class TrainUserCheckRecoderAddPage extends StatefulWidget {
  final int recoderId;
  final String remark;
  final TrainUserCheckEnum trainUserCheckEnum;

  TrainUserCheckRecoderAddPage(
      {this.recoderId,
      this.remark,
      this.trainUserCheckEnum = TrainUserCheckEnum.UserRecoder});

  @override
  _TrainUserCheckRecoderAddPageState createState() =>
      _TrainUserCheckRecoderAddPageState();
}

class _TrainUserCheckRecoderAddPageState
    extends State<TrainUserCheckRecoderAddPage> {
  TextEditingController _editingController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.remark != null) {
      _editingController.text = widget.remark;
    }
  }

  String getTitle() {
    String title = "编辑";
    switch (widget.trainUserCheckEnum) {
      case TrainUserCheckEnum.UserRecoder:
        title = "自我鉴定";
        break;
      case TrainUserCheckEnum.trainInfo:
        title = "编辑";
        break;
      case TrainUserCheckEnum.trainPeriodInfo:
        break;
        break;
      case TrainUserCheckEnum.trainCourseInfo:
        break;
      case TrainUserCheckEnum.trainResultInfo:
        break;
      case TrainUserCheckEnum.trainProblem:
        break;
      case TrainUserCheckEnum.trainPlan:
        break;
    }
    return title;
  }

  String getTips() {
    String title = "";
    switch (widget.trainUserCheckEnum) {
      case TrainUserCheckEnum.UserRecoder:
        title = "自我鉴定";
        break;
      case TrainUserCheckEnum.trainInfo:
        title = "培训基本情况";
        break;
      case TrainUserCheckEnum.trainPeriodInfo:
        title = "培训班培训学时的落实执行情况";
        break;
        break;
      case TrainUserCheckEnum.trainCourseInfo:
        title = "培训班培训课程的落实执行情况";
        break;
      case TrainUserCheckEnum.trainResultInfo:
        title = "培训班取得成效";
        break;
      case TrainUserCheckEnum.trainProblem:
        title = "该期培训班存在的问题";
        break;
      case TrainUserCheckEnum.trainPlan:
        title = "下一步搞好培训班采取的措施";
        break;
      case TrainUserCheckEnum.degreeOfMyRemark:
        title = "建议";
        break;
      case TrainUserCheckEnum.degreeOfTrainRemark:
        title = "总体评价";
        break;
      case TrainUserCheckEnum.teacherDegreeOfMyRemark:
        title = "总体评价";
        break;
    }
    return title;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(getTitle()),
      ),

      ///填充布局
      body: Container(
          color: Colors.white,
          width: double.infinity,
          height: 300,
          child: Column(
            children: [
              Container(
                padding: EdgeInsets.all(12),
                child: TextField(
                  controller: _editingController,
                  maxLength: 200,
                  maxLines: 8,
                  style: TextStyle(),
                  decoration: InputDecoration(
                      hintText: "请输入${getTips()}",
                      border: OutlineInputBorder()),
                ),
              ),
              Align(
                child: Container(
                  width: 100,
                  margin: EdgeInsets.only(right: 12),
                  child: ElevatedButton(
                    onPressed: () {
                      String text = _editingController.text;
                      commitNetDataFunction(text);
                    },
                    child: Text("提交"),
                  ),
                ),
                alignment: Alignment.centerRight,
              ),
            ],
          )),
    );
  }

  void commitNetDataFunction(String text) async {
    if (widget.trainUserCheckEnum == TrainUserCheckEnum.UserRecoder) {
      if (text.length == 0) {
        ToastUtils.showToast("请输入自我鉴定内容");
        return;
      }
      Map<String, dynamic> map = {"myRemark": text, "id": widget.recoderId};
      String url = HttpHelper.userCheckRecoderAdd;

      ResponseInfo partResponseInfo = await DioUtils.instance.postRequest(
        url: url,
        jsonMap: map,
      );
      if (partResponseInfo.success) {
      } else {
        ToastUtils.showToast(partResponseInfo.message);
        return;
      }
    } else if (widget.trainUserCheckEnum ==
            TrainUserCheckEnum.degreeOfMyRemark ||
        widget.trainUserCheckEnum == TrainUserCheckEnum.degreeOfTrainRemark) {
      if (text.length == 0) {
        ToastUtils.showToast("请输入${getTips()}");
        return;
      }
      String url = HttpHelper.trainDegreeOfStatisFacionUpdate;
      Map<String, dynamic> map = {"id": widget.recoderId};
      if (widget.trainUserCheckEnum == TrainUserCheckEnum.degreeOfMyRemark) {
        map["type"] = "degreeOfMyRemark";
        map["myRemark"] = text;
      } else {
        map["type"] = "degreeOfTrainRemark";
        map["trainRemark"] = text;
      }
      ResponseInfo partResponseInfo = await DioUtils.instance.postRequest(
        url: url,
        jsonMap: map,
      );
      if (partResponseInfo.success) {
      } else {
        ToastUtils.showToast(partResponseInfo.message);
        return;
      }
    } else if (widget.trainUserCheckEnum ==
        TrainUserCheckEnum.teacherDegreeOfMyRemark) {
      if (text.length == 0) {
        ToastUtils.showToast("请输入${getTips()}");
        return;
      }
      String url = HttpHelper.trainTeacherOfStatisFacionUpdate;
      Map<String, dynamic> map = {"id": widget.recoderId};

      map["type"] = "myRemark";
      map["myRemark"] = text;

      ResponseInfo partResponseInfo = await DioUtils.instance.postRequest(
        url: url,
        jsonMap: map,
      );
      if (partResponseInfo.success) {
      } else {
        ToastUtils.showToast(partResponseInfo.message);
        return;
      }
    } else {
      if (text.length == 0) {
        ToastUtils.showToast("请输入${getTips()}");
        return;
      }
      Map<String, dynamic> map = {"id": widget.recoderId};
      String url = HttpHelper.trainSynthesizeReportUpdate;
      switch (widget.trainUserCheckEnum) {
        case TrainUserCheckEnum.UserRecoder:
          // TODO: Handle this case.
          break;
        case TrainUserCheckEnum.trainInfo:
          map["type"] = "trainInfo";
          map["trainInfo"] = text;
          break;
        case TrainUserCheckEnum.trainPeriodInfo:
          map["type"] = "trainPeriodInfo";
          map["trainPeriodInfo"] = text;
          break;

        case TrainUserCheckEnum.trainCourseInfo:
          map["type"] = "trainCourseInfo";
          map["trainCourseInfo"] = text;
          break;
        case TrainUserCheckEnum.trainResultInfo:
          map["type"] = "trainResultInfo";
          map["trainResultInfo"] = text;
          break;
        case TrainUserCheckEnum.trainProblem:
          map["type"] = "trainProblem";
          map["trainProblem"] = text;
          break;
        case TrainUserCheckEnum.trainPlan:
          map["type"] = "trainPlan";
          map["trainPlan"] = text;
          break;
      }

      ResponseInfo partResponseInfo = await DioUtils.instance.postRequest(
        url: url,
        jsonMap: map,
      );
      if (partResponseInfo.success) {
      } else {
        ToastUtils.showToast(partResponseInfo.message);
        return;
      }
    }
    ToastUtils.showToast("编辑成功");
    Future.delayed(Duration(milliseconds: 1200), () {
      NavigatorUtils.pop(context);
    });
  }
}
